(function() {
    angular.module('app').component('dashCard', {
        css: 'app/components/dash.card.component/dash.card.component.css',
        templateUrl: 'app/components/dash.card.component/dash.card.component.html',
        controller: dashCardComponent,
        controllerAs: 'vm',
        bindings: {
            dashcardconfig: '<', //单向绑定
            catName: '=' //
        }
    });

    dashCardComponent.$inject = ['dashCardService', '$rootScope', '$scope'];

    function dashCardComponent(dashCardService, $rootScope, $scope) {
        // console.log('dash.card.component');
        var vm = this;
        // console.log(vm.dashcardconfig);
        // console.log(vm.catName);

        vm.seen = false;

        init();


        function init() {
            if (vm.catName) {
                $scope.$watch('vm.catName', function(newValue, oldValue) {
                    // console.log('dashCardComponent:change'+newValue);
                    vm.seen = false;
                    vm.infos = angular.copy(vm.dashcardconfig);
                    dashCardService.modelS(vm.infos, vm.catName).then(function() {
                        vm.seen = true;
                        console.log(vm.infos);
                    });
                });

            } else {
                var emitEv = $rootScope.$on('emit_dash_card_EV', function(e, dashCars) {
                    console.log(dashCars);
                    if (!vm.seen) {
                        vm.seen = true;
                    }

                    vm.infos = dashCars;
                });
                
                $scope.$on('$destroy', function() {
                    emitEv();
                });
            }
        }





    }
})();
